package com.tangbao.goods.service.impl;

import com.tangbao.goods.mapper.GoodsMapper;
import com.tangbao.goods.model.Goods;
import com.tangbao.goods.service.GoodsService;
import com.tangbao.util.Page;
import com.tangbao.util.ResEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * 创建人： tangbao
 * 创建时间： 2018/12/29 23:09
 * 功能：商品相关方法实现
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;


    /**
     * 描述：获取商品列表（分页）
     *
     * @param currentPage 当前页
     * @param pageSize    页大小
     *                    返回值：javax.ws.rs.core.Response
     * @throw 创建人：tangbao
     * 创建时间：2018/12/29 23:10
     * 修改人和信息：
     */
    @Override
    public Response getGoodsByAll(String sort,Integer currentPage, Integer pageSize) {
        Page page = new Page();
        if(null != currentPage) {
            page.setCurrentPage(currentPage);
        }
        if(null != pageSize) {
            page.setPageSize(pageSize);
        }

        Integer totalCount = goodsMapper.getGooodsCount();
        page.setTotalCount(totalCount);

        List<Goods> goodsList = goodsMapper.getGoodsByAll(sort,page.getStart(),page.getPageSize());
        page.setList(goodsList);
        ResEntity entity = ResEntity.okEntity().put("page", page);
        return Response.ok(entity).build();
    }

    /**
     * 描述：获取商品详情
     *
     * @param goodsId 商品id
     * 返回值：javax.ws.rs.core.Response
     * @throw 创建人：tangbao
     * 创建时间：2018/12/30 0:17
     * 修改人和信息：
     */
    @Override
    public Response getGoodsById(Integer goodsId) {
        Goods goods = goodsMapper.getGoodsById(goodsId);
        ResEntity entity = ResEntity.okEntity().put("goods", goods);
        return Response.ok(entity).build();
    }

    /**
     * 描述：获取商品介绍
     * @param goodsId 商品id
     * 返回值：javax.ws.rs.core.Response
     * @throw 创建人：tangbao
     * 创建时间：2018/12/30 0:17
     * 修改人和信息：
     */
    @Override
    public Response getDesc(Integer goodsId) {
        Goods goods = goodsMapper.getGoodsDescById(goodsId);
        ResEntity entity = ResEntity.okEntity().put("goods", goods);
        return Response.ok(entity).build();
    }

    /**
     * 描述：获取商品参数区和价格，标题等数据
     * @param goodsId 商品id
     * 返回值：javax.ws.rs.core.Response
     * @throw 创建人：tangbao
     * 创建时间：2018/12/30 0:17
     * 修改人和信息：
     */
    @Override
    public Response getInfo(Integer goodsId) {
        Goods goods = goodsMapper.getGoodsInfo(goodsId);
        ResEntity entity = ResEntity.okEntity().put("goods", goods);
        return Response.ok(entity).build();
    }


}
